package org.bot.plugin.web.event;

import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.plugin.web.util.SelectorType;
import org.bot.plugin.web.util.WebApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 检查节点是否存在
 */
@Component
@EventName("元素检查")
public class WebElementCheck implements Events {

    @Autowired
    WebApi webApi;

    @Override
    public Object exec(FlowNode node, DataStorage data) {
        String dataIndex = node.getDataIndex();
        String arg;
        if (dataIndex != null) {
            arg = data.get(dataIndex).toString();
        } else {
            arg = node.getArg();
        }
        return webApi.checkElement(webApi.getWebDriver(node),
                SelectorType.getType(node.getSelectorType()), arg);
    }

}
